﻿using System.Web;
using System.Web.Mvc;

namespace IntellectShop.Web2.Controllers
{
	[IntellectShop.Web2.Code.NoCache]
	[IntellectShop.Web2.Code.HandleErrorWithELMAH]
	public class StoreController : Controller
	{
		public ActionResult ViewLatestCourses()
		{
			var viewModel = Common.Course.RetrieveNearestCourses(4);
			return View(viewModel);
		}

		public ActionResult Details(int id)
		{
			var commodity = Common.Commodity.RetrieveByID(id);
			if (commodity == null)
			{
				throw new HttpException(404, "Page not found");
			}

			return View(commodity);
		}

		public ActionResult Redirect(int id)
		{
			Common.Commodity commodity = Common.Course.RetrieveByCommodityID(id);
			if (commodity != null)
				return RedirectToAction("Details", "Course", new { id = id });

			commodity = Common.Software.RetrieveByCommodityID(id);
			if (commodity != null)
				return RedirectToAction("Details", "Software", new { id = id });

			commodity = Common.Certification.RetrieveByCommodityID(id);
			if (commodity != null)
				return RedirectToAction("Details", "Certification", new { id = id });

			return RedirectToAction("Details", new { id = id });
		}
	}
}
